using System;
using System.Collections.Generic;
using System.Text;
using Safira.Spome.Merge.SpomeWebRef;
using System.Net;
using System.Windows.Forms;
using System.Globalization;

namespace Safira.Spome.Merge.Workers
{
    public class LoadSiteWorker
    {
        SpomeServices myProxy;
        LoadSiteResponse _response;
        LoadSiteRequest _request;
        TreeView _tree;

        /// <summary>
        /// Gets or sets the tree.
        /// </summary>
        /// <value>The tree.</value>
        public TreeView Tree
        {
            get { return _tree; }
            set { _tree = value; }
        }

        /// <summary>
        /// Gets or sets the request.
        /// </summary>
        /// <value>The request.</value>
        public LoadSiteRequest Request
        {
            get { return _request; }
            set { _request = value; }
        }

        /// <summary>
        /// Gets or sets the response.
        /// </summary>
        /// <value>The response.</value>
        public LoadSiteResponse Response
        {
            get { return _response; }
            set { _response = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LoadSiteWorker"/> class.
        /// </summary>
        /// <param name="proxy">The proxy.</param>
        public LoadSiteWorker( SpomeServices proxy )
        {
            myProxy = proxy;
        }

        /// <summary>
        /// Does the request.
        /// </summary>
        public void Work()
        {
            #region Test Request
            if ( Request == null )
            {
                return;
            }
            #endregion

            try
            {
                Response = myProxy.LoadSite( Request );
            }
            catch ( WebException e )
            {
                throw new WebException( string.Format(CultureInfo.InvariantCulture,"<Message>{0}</Message><Trace>{1}</Trace>",e.Message, e.StackTrace));
            }

            if ( Response.Success )
            {
                LoadTreeView( Tree, Response );
            }
        }

        /// <summary>
        /// Loads the tree view.
        /// </summary>
        /// <param name="tree">The tree.</param>
        /// <param name="resp">The resp.</param>
        private static void LoadTreeView( TreeView tree, LoadSiteResponse resp )
        {
            if ( tree == null )
                return;
            if ( resp == null )
                return;

            TreeNode node = ProcessSiteToTreeNodes( resp.Site );
            if ( node != null )
            {
                tree.Nodes.Clear();
                tree.Nodes.Add( node );
            }
        }

        /// <summary>
        /// Processes the site to tree nodes.
        /// </summary>
        /// <param name="site">The site.</param>
        /// <returns></returns>
        private static TreeNode ProcessSiteToTreeNodes( SharepointWeb myWeb )
        {
            if ( myWeb == null )
            {
                return null;
            }

            TreeNode baseNode = new TreeNode( string.Format( CultureInfo.InvariantCulture, "WEB:{0}({1})", myWeb.Title, myWeb.Url ) );

            if ( myWeb.Webs != null )
            {
                foreach ( SharepointWeb child in myWeb.Webs )
                {
                    TreeNode childNode = ProcessSiteToTreeNodes( child );
                    if ( childNode != null )
                    {
                        baseNode.Nodes.Add( childNode );
                    }
                }
            }
            return baseNode;
        }
    }
}
